package com.hk.service.order.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hk.commons.ConstantOrderStatus;
import com.hk.commons.HqlFilter;
import com.hk.commons.LongId;
import com.hk.commons.enm.ConfirmStatusEnum;
import com.hk.commons.enm.PayStatusEnum;
import com.hk.commons.entity.order.OrderCheck;
import com.hk.commons.entity.order.OrderInfo;
import com.hk.commons.entity.product.Company;
import com.hk.commons.entity.product.ProductInfo;
import com.hk.commons.entity.sys.SysUser;
import com.hk.commons.vo.OrderCheckMsg;
import com.hk.dao.base.impl.BaseServiceImpl;
import com.hk.service.order.IOrderCheck;
import com.hk.service.product.impl.CompanyImpl;
import com.hk.service.product.impl.ProductInfoImpl;
import com.hk.service.sys.impl.SysUserImpl;

@Service
public class OrderCheckImpl extends BaseServiceImpl<OrderCheck> implements
		IOrderCheck {

	@Autowired
	OrderInfoImpl orderService;

	@Autowired
	SysUserImpl userService;

	@Autowired
	CompanyImpl comService;

	@Autowired
	ProductInfoImpl prodService;

	@Override
	public Long add(OrderCheck model) {
		Long id = null;
		if (model.getId() == null) {
			id = LongId.get();
			model.setId(id);

			// 设置订单ID
			model.setOrderId(id);
		}
		model.setCts(System.currentTimeMillis());

		super.save(model);

		return id;
	}

	@Override
	public Long createOrderCheck(Long orderId) {
		OrderInfo orderInfo = orderService.findById(orderId);
		OrderCheck orderCheck = new OrderCheck();
		if (orderInfo != null
				&& orderInfo.getConfirmStatus() == ConstantOrderStatus.CONFIRM_STATUS__NO_CONFIRM) {
			orderCheck.setOrderId(orderInfo.getId());
			orderCheck.setCompanyId(orderInfo.getCompanyId());
			orderCheck.setPayStatus(orderInfo.getPayStatus());
			orderCheck.setConfirmStatus(orderInfo.getConfirmStatus());
			orderCheck.setUserId(orderInfo.getUserId());
		}

		Long id = add(orderCheck);
		return id;
	}

	@Override
	public OrderCheckMsg getStrInfoById(Long id) {
		OrderCheckMsg orderCheckMsg = new OrderCheckMsg();
		OrderCheck orderCheck = getById(id);
		if (orderCheck != null) {

		}

		return null;
	}

	@Override
	public List<OrderCheck> query(HqlFilter hqlFilter) {

		return null;
	}

	@Override
	public void test() {

	}

	@Override
	public Long add(OrderInfo orderInfo) {
		if(orderInfo == null ){
			return null;
		}

		Long id = null;
		// 首先判断存在不存在
		HqlFilter hqlFilter = new HqlFilter();
		hqlFilter.addEQFilter("orderId", orderInfo.getId());
		OrderCheck orderCheck = getByFilter(hqlFilter);
		
		//如果错误记录不存在则进行删除
		if (orderCheck == null) {
			orderCheck = new OrderCheck();
			if (orderInfo != null
					&& orderInfo.getConfirmStatus() == ConstantOrderStatus.CONFIRM_STATUS__NO_CONFIRM) {
				if(orderInfo.getId() != null){
					orderCheck.setOrderId(orderInfo.getId());
				}

				if(orderInfo.getCompanyId() != null){
					orderCheck.setCompanyId(orderInfo.getCompanyId());
				}

				if(orderInfo.getPayStatus() != null){
					orderCheck.setPayStatus(orderInfo.getPayStatus());
				}

				if(orderInfo.getConfirmStatus() != null){
					orderCheck.setConfirmStatus(orderInfo.getConfirmStatus());
				}
				if(orderInfo.getUserId() != null){
					orderCheck.setUserId(orderInfo.getUserId());
				}

			}
			
			id = add(orderCheck);
		}
		return id;

	}

	@Override
	public void delete(OrderCheck orderCheck) {
		super.delete(orderCheck);
	}

	@Override
	public void delete(OrderInfo orderInfo) {
		HqlFilter hqlFilter = new HqlFilter();
		hqlFilter.addEQFilter("orderId", orderInfo.getId());
		OrderCheck orderCheck = getByFilter(hqlFilter);
		delete(orderCheck);

	}

	@Override
	public OrderCheckMsg getMsg(OrderCheck orderCheck) {
		OrderCheckMsg checkMsg = new OrderCheckMsg();

		if (orderCheck != null && orderCheck.getOrderId() != null) {

			OrderInfo orderInfo = orderService
					.findById(orderCheck.getOrderId());

			if (orderInfo != null) {
				checkMsg.setOrderId(orderInfo.getId().toString());
				if(orderInfo.getSuccessAmount() != null){
					checkMsg.setAmount(orderInfo.getSuccessAmount().toString() );
				}
				SysUser user = userService.findById(orderInfo.getUserId());
				if (user != null) {
					checkMsg.setUserName(user.getRealName());
					checkMsg.setUserPhoneNum(user.getName());
				}

				if (orderInfo.getCompanyId() != null) {
					Company com = comService.findById(orderInfo.getCompanyId());
					if (com != null) {
						checkMsg.setCompanyName(com.getName());
					}
				}

				ProductInfo product = prodService.findById(orderInfo
						.getProductId());
				if (product != null) {
					checkMsg.setProductId(product.getId().toString());
					checkMsg.setProductName(product.getName());

				}
				checkMsg.setPayStatus(PayStatusEnum.getPayStatus(orderInfo
						.getPayStatus()));
				checkMsg.setConfirmStatus(ConfirmStatusEnum
						.getConfirmStatus(orderInfo.getConfirmStatus()));
				checkMsg.setOrderCreatTime(orderInfo.getOrderCreatedOn());
				
			}
		}

		return checkMsg;

	}
}
